package org.linuxgears.BouncingBall;

import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: grim
 * Date: 9 ��� 2010
 * Time: 1:00:39
 * To change this template use File | Settings | File Templates.
 */

// pattern: State
public interface BallMovingStrategy extends BallMover {
    public void move(Ball ball);

    public class Paused implements BallMovingStrategy {
        private BouncingBallModel model;

        public Paused(BouncingBallModel model) {
            this.model = model;
        }

        @Override
        public void move(Ball ball) {
            // do nothing
        }
    }

    public class Running implements BallMovingStrategy {
        private BouncingBallModel model;

        public Running(BouncingBallModel model) {
            this.model = model;
        }

        @Override
        public void move(Ball ball) {
            BallContext xtc = model.getBallContext(ball);

            if (xtc == null) return;

            Point position = xtc.getPosition();
            Rectangle bounds = xtc.getBounds();
            Point velocity = ball.getVelocity();
            Point size = new Point(ball.getSize(), ball.getSize());

            position.x += velocity.x;
            position.y += velocity.y;

            if (position.x + size.x >= bounds.width) {
                position.x = bounds.width - size.x;
                velocity.x = -velocity.x;
            }

            if (position.y + size.y >= bounds.height) {
                position.y = bounds.height - size.y;
                velocity.y = -velocity.y;
            }

            if (position.x <= 0) {
                position.x = 0;
                velocity.x = -velocity.x;
            }

            if (position.y <= 0) {
                position.y = 0;
                velocity.y = -velocity.y;
            }
        }
    }
}
